function bbEditor(to)
{
	this.to = document.getElementById(to);
}

bbEditor.prototype.addTag = function(bTag,eTag)
	{
		var lElem = this.to;
		if (document.selection)
		{ //This is IE
			lText = document.selection.createRange().text;
			lElem.focus();
			document.selection.createRange().text = bTag + lText + eTag;
		}
		else if ((lElem.selectionStart) || (lElem.selectionStart == '0'))
		{ //This is Mozilla/Gecko
			lStart = lElem.selectionStart;
			lStop = lElem.selectionEnd;
			lText = lElem.value.substring(lStart, lStop);
			
			lSubst = bTag + lText + eTag;
			lElem.value = lElem.value.substring(0, lStart) + lSubst + lElem.value.substring(lStop, lElem.value.length);
			lElem.focus();
			
			lStart += bTag.length;
			if (lText.length > 0)
				lStart += lText.length + eTag.length;
			lElem.selectionStart = lStart;
			lElem.selectionEnd = lStart;
		}
		else
		{
			lElem.value = lElem.value + bTag + eTag;
			lElem.focus();
		}
	};

bbEditor.prototype.getPos = function()
   {
     var x = 0;
     var y = 0;
     var Recurs = this.to;
     if (Recurs.offsetParent)
     {
       while (Recurs.offsetParent)
       {
         x+= this.to.offsetLeft;
	 y+= this.to.offsetTop;
         Recurs = Recurs.offsetParent;
       }
     }
     else if (this.to.x && this.to.y)
     {
       x += this.to.x;
       y += this.to.y;
     }
     return {x:x,y:y};
   }


bbEditor.prototype.preview = function()
	{
		if (!document.getElementById(this.to.id + "_preview")) {
			var pDiv = document.createElement("div");
			pDiv.id = this.to.id + "_preview";
			this.hide();
			var positions = this.getPos();
			pDiv.style.top = positions.y + "px";
			pDiv.style.left = positions.x + "px";
			pDiv.style.width = this.to.style.width;
			pDiv.style.height = this.to.style.height;
			pDiv.style.position = "absolute";
			pDiv.className = "bbeditorpreview";
			var pFrame = document.createElement("iframe");
			pFrame.className = "bbcodepreviewframe";
			var aText = this.to.value;
			while (aText.indexOf("\n") > -1) {
			aText = aText.replace("\n", "%0A%0D");
			}
			while (aText.indexOf("#") > -1) {
			aText = aText.replace("#", "%23");
			}
			while (aText.indexOf("&") > -1) {
			aText = aText.replace("&", "%26");
			}
			while (aText.indexOf("+") > -1) {
			aText = aText.replace("+", "%2b");
			}
			pFrame.src = "/Redaction/Affich/BBPreview.htm?Preview=" + aText;
			pDiv.appendChild(pFrame);
			document.body.appendChild(pDiv);
		} else {
			document.body.removeChild(document.getElementById(this.to.id + "_preview"));
			this.show();
		}
	};

bbEditor.prototype.hide =  function () {this.to.style.visibility = "hidden";};
bbEditor.prototype.show =  function () {this.to.style.visibility = "visible";};
bbEditor.prototype.clear =  function () {this.to.value = "";};
bbEditor.prototype.selectAll =  function () {this.to.focus();this.to.select();};

